package com.busionline.wotu.baselibrary.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;


import com.busionline.wotu.baselibrary.R;

/**
 * 项目名称：joke
 * 类描述：自定义dialog
 * 创建人：chenwuchao
 * 创建时间：2017/5/9 15:03
 */
public class AlertDialog extends Dialog {
    private AlertController mAlert;

    public AlertDialog(Context context, int themeResId) {
        super(context, themeResId);
        mAlert = new AlertController(this, getWindow());
    }

    /**
     * 设置文本
     *
     * @param resId
     * @param charSequence
     */
    public void setText(int resId, CharSequence charSequence) {
        mAlert.setText(resId, charSequence);
    }

    /**
     * 在集合中通过id找到控件，没有就保存进集合中
     *
     * @param resId
     * @return
     */
    public <T extends View> T getView(int resId) {
        return mAlert.getView(resId);
    }

    /**
     * 设置点击事件
     *
     * @param resId
     * @param onClickListener
     */
    public void setOnClickListener(int resId, View.OnClickListener onClickListener) {
        mAlert.setOnClickListener(resId, onClickListener);
    }


    public static class Builder {
        private final AlertController.AlertParams P;
        private final int mTheme;
        private AlertDialog mDialog;

        public Builder(Context context) {
            this(context, R.style.dialog);
        }

        public Builder(Context context, int themeResId) {
            P = new AlertController.AlertParams(context, themeResId);
            mTheme = themeResId;
        }

        /**
         * 设置布局
         *
         * @param layoutResId
         * @return
         */
        public Builder setContentView(int layoutResId) {
            P.mView = null;
            P.mViewLayoutResId = layoutResId;
            return this;
        }

        /**
         * 设置布局
         *
         * @param view
         * @return
         */
        public Builder setContentView(View view) {
            P.mView = view;
            P.mViewLayoutResId = 0;
            return this;
        }

        /**
         * 取消监听
         *
         * @param onCancelListener
         * @return
         */
        public Builder setOnCancelListener(OnCancelListener onCancelListener) {
            P.mOnCancelListener = onCancelListener;
            return this;
        }

        /**
         * 销毁监听
         *
         * @param onDismissListener
         * @return
         */
        public Builder setOnDismissListener(OnDismissListener onDismissListener) {
            P.mOnDismissListener = onDismissListener;
            return this;
        }

        /**
         * 键盘按下监听
         *
         * @param onKeyListener
         * @return
         */
        public Builder setOnKeyListener(OnKeyListener onKeyListener) {
            P.mOnKeyListener = onKeyListener;
            return this;
        }


        /**
         * 设置文本
         *
         * @param resId 资源id
         * @param text  文本
         * @return
         */
        public Builder setText(int resId, CharSequence text) {
            P.textArrays.put(resId, text);
            return this;
        }

        /**
         * 设置点击事件
         *
         * @param resId
         * @param onClickListener
         * @return
         */
        public Builder setOnClickListener(int resId, View.OnClickListener onClickListener) {
            P.clickArrays.put(resId, onClickListener);
            return this;
        }


        /**
         * 设置全屏
         *
         * @return
         */
        public Builder setFullWidth() {
            P.mWidth = ViewGroup.LayoutParams.MATCH_PARENT;
            return this;
        }


        /**
         * 设置宽高
         *
         * @param width  宽度
         * @param height 高度
         * @return
         */
        public Builder setWidthAndHeight(int width, int height) {
            P.mWidth = width;
            P.mHeight = height;
            return this;
        }


        /**
         * 设置从底部弹出
         *
         * @param isAnimation 是否有动画
         * @return
         */
        public Builder fromBottom(boolean isAnimation) {
            if (isAnimation) {
                P.mAnimation = R.style.dialog_form_bottm_anim;
            }
            P.mGravity = Gravity.BOTTOM;
            return this;
        }

        /**
         * 设置默认动画
         *
         * @return
         */
        public Builder addDefaultAnimation() {
//            P.mAnimation = R.style.dialog_scale_anim;
            return this;
        }

        /**
         * 设置自定义动画
         *
         * @param styleAnimation
         * @return
         */
        public Builder addAnimation(int styleAnimation) {
            P.mAnimation = styleAnimation;
            return this;
        }

        /**
         * 获取dialog
         *
         * @return
         */
        public AlertDialog getDialog() {
            if (mDialog == null) {
                throw new IllegalArgumentException("please create dialog first!");
            }
            return mDialog;
        }


        /**
         * 创建dialog
         *
         * @return
         */
        public AlertDialog create() {
            final AlertDialog dialog = new AlertDialog(P.mContext, mTheme);
            P.apply(dialog.mAlert);
            dialog.setCancelable(P.mCancelable);
            if (P.mCancelable) {
                dialog.setCanceledOnTouchOutside(true);
            }
            dialog.setOnCancelListener(P.mOnCancelListener);
            dialog.setOnDismissListener(P.mOnDismissListener);
            if (P.mOnKeyListener != null) {
                dialog.setOnKeyListener(P.mOnKeyListener);
            }
            //返回dialog
            mDialog = dialog;
            return dialog;
        }

        /**
         * 显示dialog
         *
         * @return
         */
        public AlertDialog show() {
            final AlertDialog dialog = create();
            dialog.show();
            return dialog;
        }

    }
}
